package cc.mallet.topics.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class AddLabelDialog extends JDialog {

	private static final long serialVersionUID = 2014091801L;

	private JLabel topicLabel;
	private JTextField labelField;

	private JButton okButton;
	private JButton cancelButton;
	private JPanel contentPane;

	public void init() {
		setModalityType(ModalityType.APPLICATION_MODAL);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

		contentPane = new JPanel(new BorderLayout());
		setContentPane(contentPane);

		topicLabel = new JLabel("topic topic topic topic topic topic");
		labelField = new JTextField(20);
		JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		centerPanel.add(topicLabel);
		centerPanel.add(labelField);

		okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (labelField.getText().isEmpty()) {
					JOptionPane.showConfirmDialog(contentPane,
							"Please enter a label", "Missing Label",
							JOptionPane.OK_CANCEL_OPTION);
					return;
				}

				setVisible(false);

			}
		});
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setVisible(false);

			}
		});

		JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		southPanel.add(okButton);
		southPanel.add(cancelButton);

		contentPane.add(centerPanel, BorderLayout.CENTER);
		contentPane.add(southPanel, BorderLayout.SOUTH);

	}

	public String getLabel() {
		return labelField.getText().trim();
	}

	public void setTopic(String topic) {
		topicLabel.setText(topic);
		labelField.setText("");
	}

}
